package com.wangfei.test.filehandler.impl;

import com.wangfei.test.filehandler.access.IStringReplace;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class AbstractIStringReplace implements IStringReplace {

    private  Pattern pattern;
    private  String value;

    AbstractIStringReplace(){};

    AbstractIStringReplace(String patternStr, String value) {
        this.pattern = Pattern.compile(patternStr);
        this.value = value;
    }

    @Override
    public String replace(String oldString) throws Exception{
        if(oldString == null) {
            throw new Exception("error inputting");
        }
        Matcher matcher = pattern.matcher(oldString);
        if(matcher.find()){
            return matcher.replaceAll(this.value);
        }
        return oldString;
    }
}
